package com.tom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Driver;

public class Member {
	String id;
	String password1;
	String password2;
	String name_c;
	String name_a;
	String brith;
	String tel_area1;
	String telephone1;
	String telephone1_p;
	String mobile1;
	String tel_area2;
	String telephone2;
	String telephone2_p;
	String mobile2;
	String address1;
	String address2;
	String original;
	String professional;
	String company_num;
	String company_name;
	String company_job;
	String company_tel_area;
	String company_tel;
	String company_tel_p;
	String contact_name;
	String contact_keep;
	String contact_area;
	String contact_tel;
	String contact_tel_p;
	String email;
	
	private String useridMessage = null;
	private String pwMessage = null;
	private String nameMessage =null;
	private String address1Message =null;
	private String address2Message =null;
	private String emailMessage = null;
	
	String loginMessage;
	private boolean login;
	String referer;
	
	public Member(){
		
	}
	
	
	
	public Member(String id, String password1) {
		super();
		this.id = id;
		this.password1 = password1;
	}



	public Member(String id, String password1, String password2, String name_c,
			String name_a, String brith, String tel_area1, String telephone1,
			String telephone1_p, String mobile1, String tel_area2,
			String telephone2, String telephone2_p, String mobile2,
			String address1, String address2, String original,
			String professional, String company_num, String company_name,
			String company_job, String company_tel_area, String company_tel,
			String company_tel_p, String contact_name, String contact_keep,
			String contact_area, String contact_tel, String contact_tel_p,
			String email) {
		super();
		this.id = id;
		this.password1 = password1;
		this.password2 = password2;
		this.name_c = name_c;
		this.name_a = name_a;
		this.brith = brith;
		this.tel_area1 = tel_area1;
		this.telephone1 = telephone1;
		this.telephone1_p = telephone1_p;
		this.mobile1 = mobile1;
		this.tel_area2 = tel_area2;
		this.telephone2 = telephone2;
		this.telephone2_p = telephone2_p;
		this.mobile2 = mobile2;
		this.address1 = address1;
		this.address2 = address2;
		this.original = original;
		this.professional = professional;
		this.company_num = company_num;
		this.company_name = company_name;
		this.company_job = company_job;
		this.company_tel_area = company_tel_area;
		this.company_tel = company_tel;
		this.company_tel_p = company_tel_p;
		this.contact_name = contact_name;
		this.contact_keep = contact_keep;
		this.contact_area = contact_area;
		this.contact_tel = contact_tel;
		this.contact_tel_p = contact_tel_p;
		this.email = email;
	}

	
	
	

	public Member(String id, String password1, String password2, String name_c,
			String name_a, String brith, String tel_area1, String telephone1,
			String telephone1_p, String mobile1, String tel_area2,
			String telephone2, String telephone2_p, String mobile2,
			String address1, String address2, String original,
			String professional, String company_num, String company_name,
			String company_job, String company_tel_area, String company_tel,
			String company_tel_p, String contact_name, String contact_keep,
			String contact_area, String contact_tel, String contact_tel_p,
			String email, String useridMessage, String pwMessage,
			String nameMessage, String address1Message, String address2Message,String emailMessage,
			String loginMessage, boolean login, String referer) {
		super();
		this.id = id;
		this.password1 = password1;
		this.password2 = password2;
		this.name_c = name_c;
		this.name_a = name_a;
		this.brith = brith;
		this.tel_area1 = tel_area1;
		this.telephone1 = telephone1;
		this.telephone1_p = telephone1_p;
		this.mobile1 = mobile1;
		this.tel_area2 = tel_area2;
		this.telephone2 = telephone2;
		this.telephone2_p = telephone2_p;
		this.mobile2 = mobile2;
		this.address1 = address1;
		this.address2 = address2;
		this.original = original;
		this.professional = professional;
		this.company_num = company_num;
		this.company_name = company_name;
		this.company_job = company_job;
		this.company_tel_area = company_tel_area;
		this.company_tel = company_tel;
		this.company_tel_p = company_tel_p;
		this.contact_name = contact_name;
		this.contact_keep = contact_keep;
		this.contact_area = contact_area;
		this.contact_tel = contact_tel;
		this.contact_tel_p = contact_tel_p;
		this.email = email;
		this.useridMessage = useridMessage;
		this.pwMessage = pwMessage;
		this.nameMessage = nameMessage;
		this.address1Message = address1Message;
		this.address2Message = address2Message;
		this.emailMessage = emailMessage;
		this.loginMessage = loginMessage;
		this.login = login;
		this.referer = referer;
	}
	
	
	
	public boolean validate(){
		boolean valid = true;
		
		
		if (id.length()!=10 || id.length()==0){
			valid = false;
			useridMessage = "使用者帳號長度不符合";
		}
		if (password1.length()<6 || password1.length()>16){
			valid = false;
			pwMessage = "密碼長度不符合,密碼長度請輸入6~16之間的英文及數字混合";
		}
		if (password2.length()<6 || password2.length()>16){
				valid = false;
				pwMessage = "密碼長度不符合";
		}
		if (!password1.equals(password2)){
			valid = false;
			pwMessage = "確認密碼需與密碼一致";
		}
		if (name_c.length()==0){
			valid = false;
			nameMessage = "請輸入中文姓名";
		}
		if (address1.length()==0){
			valid = false;
			address1Message = "請輸入戶藉地址";
		}
		if (address2.length()==0){
			valid = false;
			address2Message = "請輸入聯絡地址";
		}
		
		if (email.indexOf("@")==-1){
			valid = false;
			emailMessage = "電子郵件格式錯誤";
		}
		if (valid){
			Connection conn = null;
			PreparedStatement pstmt = null;
			
			try {
			DriverManager.registerDriver(new Driver());
			conn = DriverManager.getConnection(
				"jdbc:mysql://j.snpy.org/j102u07?useUnicode=true&characterEncoding=UTF-8", "j102u07", "j102u07.989");
			String sql = "Insert into member(id,password1,password2,name_c,name_a,brith,tel_area1,telephone1,telephone1_p,mobile1,tel_area2,telephone2,telephone2_p,mobile2,address1,address2,original,professional,company_num,company_name,company_job,company_tel_area,company_tel,company_tel_p,contact_name,contact_keep,contact_area,contact_tel,contact_tel_p,email)values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setString(2, password1);
			pstmt.setString(3, password2);
			pstmt.setString(4, name_c);
			pstmt.setString(5, name_a);
			pstmt.setString(6, brith);
			pstmt.setString(7, tel_area1);
			pstmt.setString(8, telephone1);
			pstmt.setString(9, telephone1_p);
			pstmt.setString(10, mobile1);
			pstmt.setString(11, tel_area2);
			pstmt.setString(12, telephone2);
			pstmt.setString(13, telephone2_p);
			pstmt.setString(14, mobile2);
			pstmt.setString(15, address1);
			pstmt.setString(16, address2);
			pstmt.setString(17, original);
			pstmt.setString(18, professional);
			pstmt.setString(19, company_num);
			pstmt.setString(20, company_name);
			pstmt.setString(21, company_job);
			pstmt.setString(22, company_tel_area);
			pstmt.setString(23, company_tel);
			pstmt.setString(24, company_tel_p);
			pstmt.setString(25, contact_name);
			pstmt.setString(26, contact_keep);
			pstmt.setString(27, contact_area);
			pstmt.setString(28, contact_tel);
			pstmt.setString(29, contact_tel_p);
			pstmt.setString(30, email);
			System.out.println(sql);
			int rowCount = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			if(conn!=null)
			{
				try
				{
					pstmt.close();
					conn.close();
				}
				catch (SQLException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	return valid ;
	}
	
	public boolean login(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		login = false;
		try {
			DriverManager.registerDriver(new Driver());
			conn = DriverManager.getConnection(
				"jdbc:mysql://j.snpy.org/j102u07?useUnicode=true&characterEncoding=UTF-8", "j102u07", "j102u07.989");
			String sql = "select * from member where id=? and password1=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setString(2, password1);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()){
				password2 = rs.getString("password2");
				name_c = rs.getString("name_c");
				name_a = rs.getString("name_a");
				brith = rs.getString("brith");
				tel_area1 = rs.getString("tel_area1");
				telephone1 = rs.getString("telephone1");
				telephone1_p = rs.getString("telephone1_p");
				mobile1=rs.getString("mobile1");
				tel_area2 = rs.getString("tel_area2");
				telephone2 = rs.getString("telephone2");
				telephone2_p = rs.getString("telephone2_p");
				mobile2 = rs.getString("mobile2");
				address1 = rs.getString("address1");
				address2 = rs.getString("address2");
				original = rs.getString("original");
				professional = rs.getString("professional");
				company_num = rs.getString("company_num");
				company_name = rs.getString("company_name");
				company_job = rs.getString("company_job");
				company_tel_area = rs.getString("company_tel_area");
				company_tel = rs.getString("company_tel");
				company_tel_p = rs.getString("company_tel_p");
				contact_name = rs.getString("contact_name");
				contact_keep = rs.getString("contact_keep");
				contact_area = rs.getString("contact_area");
				contact_tel = rs.getString("contact_tel");
				contact_tel_p = rs.getString("contact_tel_p");
				email = rs.getString("email");
				login = true;
				
			}else{
				loginMessage = "登入失敗";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (conn!=null){
				try {
					pstmt.close();
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return login;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getName_c() {
		return name_c;
	}

	public void setName_c(String name_c) {
		this.name_c = name_c;
	}

	public String getName_a() {
		return name_a;
	}

	public void setName_a(String name_a) {
		this.name_a = name_a;
	}

	public String getBrith() {
		return brith;
	}

	public void setBrith(String brith) {
		this.brith = brith;
	}

	public String getTel_area1() {
		return tel_area1;
	}

	public void setTel_area1(String tel_area1) {
		this.tel_area1 = tel_area1;
	}

	public String getTelephone1() {
		return telephone1;
	}

	public void setTelephone1(String telephone1) {
		this.telephone1 = telephone1;
	}

	public String getTelephone1_p() {
		return telephone1_p;
	}

	public void setTelephone1_p(String telephone1_p) {
		this.telephone1_p = telephone1_p;
	}

	public String getMobile1() {
		return mobile1;
	}

	public void setMobile1(String mobile1) {
		this.mobile1 = mobile1;
	}

	public String getTel_area2() {
		return tel_area2;
	}

	public void setTel_area2(String tel_area2) {
		this.tel_area2 = tel_area2;
	}

	public String getTelephone2() {
		return telephone2;
	}

	public void setTelephone2(String telephone2) {
		this.telephone2 = telephone2;
	}

	public String getTelephone2_p() {
		return telephone2_p;
	}

	public void setTelephone2_p(String telephone2_p) {
		this.telephone2_p = telephone2_p;
	}

	public String getMobile2() {
		return mobile2;
	}

	public void setMobile2(String mobile2) {
		this.mobile2 = mobile2;
	}

	public String getAddress1() {
		return address1;
	}

	public void setAddress1(String address1) {
		this.address1 = address1;
	}

	public String getAddress2() {
		return address2;
	}

	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	public String getOriginal() {
		return original;
	}

	public void setOriginal(String original) {
		this.original = original;
	}

	public String getProfessional() {
		return professional;
	}

	public void setProfessional(String professional) {
		this.professional = professional;
	}

	public String getCompany_num() {
		return company_num;
	}

	public void setCompany_num(String company_num) {
		this.company_num = company_num;
	}

	public String getCompany_name() {
		return company_name;
	}

	public void setCompany_name(String company_name) {
		this.company_name = company_name;
	}

	public String getCompany_job() {
		return company_job;
	}

	public void setCompany_job(String company_job) {
		this.company_job = company_job;
	}

	public String getCompany_tel_area() {
		return company_tel_area;
	}

	public void setCompany_tel_area(String company_tel_area) {
		this.company_tel_area = company_tel_area;
	}

	public String getCompany_tel() {
		return company_tel;
	}

	public void setCompany_tel(String company_tel) {
		this.company_tel = company_tel;
	}

	public String getCompany_tel_p() {
		return company_tel_p;
	}

	public void setCompany_tel_p(String company_tel_p) {
		this.company_tel_p = company_tel_p;
	}

	public String getContact_name() {
		return contact_name;
	}

	public void setContact_name(String contact_name) {
		this.contact_name = contact_name;
	}

	public String getContact_keep() {
		return contact_keep;
	}

	public void setContact_keep(String contact_keep) {
		this.contact_keep = contact_keep;
	}

	public String getContact_area() {
		return contact_area;
	}

	public void setContact_area(String contact_area) {
		this.contact_area = contact_area;
	}

	public String getContact_tel() {
		return contact_tel;
	}

	public void setContact_tel(String contact_tel) {
		this.contact_tel = contact_tel;
	}

	public String getContact_tel_p() {
		return contact_tel_p;
	}

	public void setContact_tel_p(String contact_tel_p) {
		this.contact_tel_p = contact_tel_p;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUseridMessage() {
		return useridMessage;
	}

	public void setUseridMessage(String useridMessage) {
		this.useridMessage = useridMessage;
	}

	public String getPwMessage() {
		return pwMessage;
	}

	public void setPwMessage(String pwMessage) {
		this.pwMessage = pwMessage;
	}

	public String getNameMessage() {
		return nameMessage;
	}

	public void setNameMessage(String nameMessage) {
		this.nameMessage = nameMessage;
	}

	public String getAddress1Message() {
		return address1Message;
	}

	public void setAddress1Message(String address1Message) {
		this.address1Message = address1Message;
	}
	public String getAddress2Message() {
		return address2Message;
	}
	
	public void setAddress2Message(String address2Message) {
		this.address2Message = address2Message;
	}

	public String getEmailMessage() {
		return emailMessage;
	}

	public void setEmailMessage(String emailMessage) {
		this.emailMessage = emailMessage;
	}

	public String getLoginMessage() {
		return loginMessage;
	}

	public void setLoginMessage(String loginMessage) {
		this.loginMessage = loginMessage;
	}

	public boolean isLogin() {
		return login;
	}

	public void setLogin(boolean login) {
		this.login = login;
	}

	public String getReferer() {
		if (referer!=null && referer.substring(referer.lastIndexOf("/")).equals("/index.jsp")){
			referer = null;
		}
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
		System.out.println("setReferer:"+referer);
	}
	
	public List<String> getAreaCodeList() {
        List<String> tmpList = new ArrayList<String>();
        tmpList.add("02");
        tmpList.add("03");
        tmpList.add("037");
        tmpList.add("04");
        tmpList.add("049");
        tmpList.add("05");
        tmpList.add("06");
        tmpList.add("07");
        tmpList.add("08");
        tmpList.add("082");
        tmpList.add("0826");
        tmpList.add("0827");
        tmpList.add("0836");
        tmpList.add("089");
        return tmpList;
    }
	
	public List<String> getEducationBackgroundList() {
        List<String> tmpList = new ArrayList<String>();
        tmpList.add("其他");
        tmpList.add("高中以下");
        tmpList.add("高中職");
        tmpList.add("專科學院");
        tmpList.add("大學");
        tmpList.add("碩士");
        tmpList.add("博士");    
        return tmpList;
    }
	public List<String> getJobList() {
        List<String> tmpList = new ArrayList<String>();
        tmpList.add("資訊業");
        tmpList.add("電子業");
        tmpList.add("金融業");
        tmpList.add("傳撥娛樂業");
        tmpList.add("醫療服務業");
        tmpList.add("法律服務業");
        tmpList.add("土木建築業");
        tmpList.add("製造業");
        tmpList.add("貿易業");
        tmpList.add("出版業");
        tmpList.add("旅遊業");
        tmpList.add("運輸業");
        tmpList.add("百貨零售業");
        tmpList.add("服務業");
        tmpList.add("食品業");
        tmpList.add("傳銷相關業");
        tmpList.add("農林畜牧業");
        tmpList.add("學生");
        tmpList.add("其他");    
        return tmpList;
    }

}
